Creating the unit test for an ASP.NET web site or
application is similar to the one we created for the normal class
library. The ASP.NET unit test is used for testing the methods or the
business logic used for the ASP.NET site. The only difference is the
additional attributes added to the methods to identify the URL and the
Host. The ASP.NET unit test can be run using IIS web server or the
development web server. If it is on the IIS server we can choose the
user identity with which the unit test should run. The default identity
depends on the version of the IIS server and the operating system.
Let us consider a simple user
registration page created using ASP.NET using Visual Studio 2008. Below
is the UI for the user to get registered.
This web application runs on
the local development server. The application has two methods. One is to
get the user details from the user interface and create a new user
object and the other is to just display the user name on the screen
after submit. The application also has a class file for the user
information.
protected void BtnSubmit_Click(object sender, EventArgs e)
unit testingfor ASP.NET application{
User usr = new User();
GetUserDetails(usr);
LabelOutput.Text = "Hello " + usr.FirstName + " " +
usr.LastName + " you are sucessfully registered with the site";
}
public User GetUserDetails(User user)
{
user.FirstName = TextBoxFirstName.Text;
user.LastName = TextBoxLastName.Text;
user.MiddleName = TextBoxMiddleName.Text;
user.Address = TextBoxCity.Text;
user.Street = TextBoxStreet.Text;
user.City = TextBoxCity.Text;
user.Country = TextBoxCountry.Text;
user.Email = TextBoxEmail.Text;
user.Phone = TextBoxPhone.Text;
return user;
}
Before generating the unit
test for the web application, let us build and run the application once
to make sure it runs as expected.
Now to generate the unit test, open the code file of the web page, then right-click and select the Create Unit Tests…
option which identifies all the classes and the methods for which the
unit test can be generated as shown in the screenshot below:
Select the methods and the
user class for which the unit test can be generated and tested. Now
Visual Studio creates the unit test class file for the new test with the
required attributes and the base code for test. The unit test code for
the two methods of the web page would be:
[TestMethod()]
unit testingfor ASP.NET application[HostType("ASP.NET")]
[AspNetDevelopmentServerHost("C:\\Workspace\\UnitTest\\ SampleAppforUnitTest\\SampleAppforUnitTest", "/")]
[UrlToTest("http://localhost:11961/")]
[DeploymentItem("SampleAppforUnitTest.dll")]
public void BtnSubmit_ClickTest()
{
_Default_Accessor target = new _Default_Accessor(); // TODO: Initialize to an appropriate value
object sender = null; // TODO: Initialize to an //appropriate value
EventArgs e = null; // TODO: Initialize to an //appropriate value
target.BtnSubmit_Click(sender, e);
Assert.Inconclusive("A method that does not return a value cannot be verified.");
}
///
///A test for GetUserDetails
///
[TestMethod()]
[HostType("ASP.NET")]
[AspNetDevelopmentServerHost("C:\\Workspace\\UnitTest\\ SampleAppforUnitTest\\SampleAppforUnitTest", "/")]
[UrlToTest("http://localhost:11961/")]
public void GetUserDetailsTest()
{
_Default target = new _Default(); // TODO: Initialize //to an appropriate value
User user = null; // TODO: Initialize to an //appropriate value
User expected = null; // TODO: Initialize to an //appropriate value
User actual;
actual = target.GetUserDetails(user);
Assert.AreEqual(expected, actual);
Assert.Inconclusive("Verify the correctness of this test method.");
}
There are different attributes added to the unit test methods:
TestMethod—attribute to identify the whether a method is a unit test method.
HostType—the type of the host that takes care of running the unit test. In this case it is the ASP.NET.
AspNetDevelopmentServerHost—this specifies the settings to be used when the development server is used as the host for the unit testing.
UrlToTest—specifies the application URL to be used for the test context.
DeploymentItem—this is to specify the items like files and folders to be deployed before the test.
After creating the test and
setting the environment, set the expected values and include the
required assert method for comparing the values and passing the test.
For example, change the GetUserDetailsTest method as below:
[TestMethod()]
[HostType("ASP.NET")]
[AspNetDevelopmentServerHost("C:\\Workspace\\UnitTest\\ SampleAppforUnitTest\\SampleAppforUnitTest", "/")]
[UrlToTest("http://localhost:11961/")]
public void GetUserDetailsTest()
{
_Default target = new _Default();
User user = new User();
User expected = new User();
expected.FirstName = "Subha";
expected.LastName = "S";
User actual;
actual = target.GetUserDetails(user);
Assert.AreEqual(expected.FirstName, actual.FirstName);
}
The test will pass if the value of FirstName and LastName properties of the expected object and the actual object returned by the method are equal. Right-click on the method and select Run Tests...
We can use the different assert methods and test all different scenarios of the unit test with different expected values.